using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using VTESCore;

namespace VTESGameLogicWindows
{
	public class Methuselah : 
		IPlayer, 
		IVTESGameEntity
	{
		#region Consts
		static readonly int MAX_METHUSELAH_BLOOD = 30;
		#endregion

		#region Members
		// Name and gamer profile
		string m_Name;
		Gamer m_Gamer;

		// Library and Crypt
		Stack<LibraryCard> m_Library;
		Stack<CryptCard> m_Crypt;
	
		// Blood
		int m_Blood = MAX_METHUSELAH_BLOOD;

		// Hand
		List<LibraryCard> m_Hand = new List<LibraryCard>();

		// Controlled, Uncontrolled and Torpor
		List<Minion> m_Controlled = new List<Minion>();
		List<Minion> m_Uncontrolled = new List<Minion>();
		List<Minion> m_Torpor = new List<Minion>();

		// Masters and other cards in play
		List<LibraryCard> m_CardsInPlay = new List<LibraryCard>();

		// Edge
		bool m_HasEdge;
		#endregion

		#region Properties
		public string Name
		{
			get { return m_Name; }
		}

		public List<LibraryCard> Hand
		{
			get { return m_Hand; }
		}

		public List<Minion> Controlled
		{
			get { return m_Controlled; }
		}

		public List<Minion> Uncontrolled
		{
			get { return m_Uncontrolled; }
		}

		public int Blood
		{
			get { return m_Blood; }
			set { m_Blood = value; }
		}
		#endregion

		#region Construction
		public Methuselah( Gamer gamer)
		{
			m_Name = gamer.Gamertag;
			m_Gamer = gamer;
		}
		public Methuselah(string name)
		{
			m_Name = name;
		}

		public Methuselah(VTESDeck deck)
		{
			m_Library = new Stack<LibraryCard>( deck.LibraryCards );
			m_Crypt = new Stack<CryptCard>( deck.CryptCards );
		}
		#endregion

		#region IPlayer Members

		public void PerformUntapPhase()
		{
			if(m_HasEdge && m_Blood < MAX_METHUSELAH_BLOOD)
			{
				m_Blood++;
			}
			foreach(Minion controlled_minion in m_Controlled)
			{
				controlled_minion.Tapped = false;
			}
			foreach(Minion torpored_minion in m_Torpor)
			{
				torpored_minion.Tapped = false;
			}
		}

		public void PerformMasterPhase( LibraryCard master_card )
		{
			m_CardsInPlay.Add( master_card );
		}

		public void PerformMinionPhase( Minion acting_minion, MinionActions action_type, VTESCard action_card, IVTESGameEntity target)
		{
			
		}

		public void PerformInfluencePhase(int available_transfers, IVTESGameEntity source, IVTESGameEntity target)
		{
			source.Blood -= available_transfers;
			target.Blood += available_transfers;
		}

		public void PerformDiscardPhase(LibraryCard discard_card)
		{
			if(discard_card != null)
			{
				m_Hand.Remove( discard_card );
				DrawCard();
			}
		}

		public bool Alive
		{
			get { return m_Blood > 0; }
		}

		#endregion

		#region Methods

		public void DrawCard()
		{
			LibraryCard drawn_card = m_Library.Pop();
		}

		#endregion
	}
}
